import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';

const typeToMenuName: { [key: number]: string } = {
    1: '凯笙导航',
    2: 'AI工具',
    3: '热点资讯',
};

async function handler(req: NextRequest) {
    const { type } = await req.json();

    if (!type || !typeToMenuName[type]) {
        throw new Error('Invalid type specified');
    }

    const menuName = typeToMenuName[type];

    const categories = await prisma.category.findMany({
        where: {
            menu: {
                name: menuName,
            },
        },
    });

    return {
        data: categories,
    };
}

export const POST = withMiddleware(handler, { beforeCallback: [authenticateRequest] });